package br.com.j7.jdesk.ui.bean;
import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.j7.jdesk.library.Lang;
import br.com.j7.jdesk.persistence.entity.Enterprises;
import br.com.j7.jdesk.persistence.entity.Users;

/**
 * 
 * @author Edivando Alves
 *
 */
public abstract class WebContext {

//	protected EntityManager getManager(){
//		FacesContext fc = FacesContext.getCurrentInstance();
//		ExternalContext ec = fc.getExternalContext();
//		HttpServletRequest request = (HttpServletRequest) ec.getRequest();
//		return (EntityManager) request.getAttribute("EntityManager");
//	}
	
	protected static HttpSession getCurrentSession() {
		ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
		return (HttpSession) ec.getSession(false);
	}
	
	protected Enterprises getSessionEnterprise(){
		return (Enterprises) getCurrentSession().getAttribute("enterprise");
	}
	
	protected Users getSessionUser(){
		return (Users) getCurrentSession().getAttribute("user");
	}
	
	protected void addMessage(String langMessage) {
		addMessage(langMessage, "");
	}
	
	protected void addMessage(String langTitle, String message) {
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(Lang.trans(langTitle),message));
	}
}
